﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Sunny.UI;

namespace xiaolanshu
{
    public partial class 护肤查看评价 : Form
    {
        public SqlConnection conn;
        private UIBarOption option;
        public 护肤查看评价()
        {
            InitializeComponent();
            conn = SQLTools.conn;
            // 配置参数
            option = new UIBarOption();
            // 配置标题
            option.Title = new UITitle();
            // 主标题
            option.Title.Text = comboBox1.Text;
            // 副标题
            option.Title.SubText = "评价指标柱状图";

            uiBarChart1.ChartStyleType = UIChartStyleType.Dark;

            // 设置图例
            option.Legend = new UILegend();
            // 图例水平布局
            option.Legend.Orient = UIOrient.Horizontal;
            // 图例放置在左上角
            option.Legend.Top = UITopAlignment.Top;
            option.Legend.Left = UILeftAlignment.Left;
            // 两个图例分别是Bar1和Bar2
            option.Legend.AddData("评分");
            
            
            // 设置系列
            UIBarSeries series = new UIBarSeries();
            
            // 第一系列Bar1
            series.Name = "评分";
            
            series.AddData(1.1);
            series.AddData(1.1);
            series.AddData(1.1);
            series.AddData(1.1);
            
            option.Series.Add(series);
            
            // 设置横坐标内容
            // 第一系列和第二系列的元素交织分布
            // 所以Mon是指第一和二系列的第一个元素的横坐标
            // Tue指第一和二系列的第二个元素的横坐标
            option.XAxis.Data.Add("整体评价");
            option.XAxis.Data.Add("美白效果");
            option.XAxis.Data.Add("保湿控油效果");
            option.XAxis.Data.Add("吸收效果");
            

            // 辅助ToolTip是否可见
            option.ToolTip.Visible = true;
            // Y轴的刻度
            option.YAxis.Scale = true;
            // XY轴的单位
            option.XAxis.Name = "评价指标";
            option.YAxis.Name = "数值";
            // 标记处上下限（数值超过了也没事）
            option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Red, Name = "上限", Value = 100 });
            option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Gold, Name = "下限", Value = 0 });
            // 更新坐标轴
            uiBarChart1.SetOption(option);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = @"select AVG(整体评价)as pj,AVG(美白效果) as mb,AVG(保湿控油效果) as bs,AVG(吸收效果) as xs from 护肤反馈 where 商品名称=@商品名称 ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add("@商品名称", SqlDbType.VarChar, 50);
            cmd.Parameters["@商品名称"].Value = comboBox1.Text;
            
            UIBarSeries series = option.Series[0];
            List<double> datasList = series.Data;
            double[] datas = datasList.ToArray();
            series.Clear();



            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.Read())
            {
                
                series.Name = "评分";
                series.AddData(double.Parse(rd["pj"].ToString()));
                series.AddData(double.Parse(rd["mb"].ToString()));
                series.AddData(double.Parse(rd["bs"].ToString()));
                series.AddData(double.Parse(rd["xs"].ToString()));
                
            }
            rd.Close();
            option.Series[0] = series;
            uiBarChart1.SetOption(option);
            conn.Close();
        }

        private void 护肤查看评价_Load(object sender, EventArgs e)
        {
            string sql = "select 商品名称 from 护肤";
            SqlCommand cmd = new SqlCommand(sql, conn);
            if (cmd.Connection.State == ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                comboBox1.Items.Add(rd[0]);
            }
            rd.Close();
            conn.Close();
        }
    }
}
